package com.healthy.sport.demo.utils;

import com.healthy.sport.demo.dto.DatasourceSQL;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ln
 * @Description: 从数据库的数据源表中查询出所有的数据源
 * @date 2020/11/10
 */
@Slf4j
public class ConUtil {

    public static final String dburl = "jdbc:mysql://127.0.0.1:3306/sport_data?useUnicode=true&characterEncoding=UTF-8";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String pwd = "lndaisy";

    public static Connection conn = null;
    public static Statement statement = null;

    public static List<DatasourceSQL> get() {
        ArrayList<DatasourceSQL> list = new ArrayList<>();
        DatasourceSQL defaultDatasource ;
        String str = null;
        String sql = "select * from datasource";
        try {
            Class.forName(name);
            conn = DriverManager.getConnection(dburl, user, pwd);
            statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            int row = resultSet.getRow();//行数
            log.info("行数{}",row);
            while (resultSet.next()) {//向下移动一行
                defaultDatasource = new DatasourceSQL();
                defaultDatasource.setSchool_id(resultSet.getString("school_id"));
                defaultDatasource.setDiverClassName(resultSet.getString("driver-class-name"));
                defaultDatasource.setUrl(resultSet.getString("url"));
                defaultDatasource.setUsername(resultSet.getString("username"));
                defaultDatasource.setPassword(resultSet.getString("password"));
                list.add(defaultDatasource);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void main(String[] args) {
        List<DatasourceSQL> list = ConUtil.get();
        for (DatasourceSQL datasource : list) {
            System.out.println(datasource);
        }

    }

}
